package com.diana.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.PageUtils;
import com.diana.common.StatusEnums;
import com.diana.common.TokenUtils;
import com.diana.common.Utils;
import com.diana.entity.ManagerMain;
import com.diana.entity.ManagerRole;
import com.diana.mapper.ManagerMainMapper;
import com.diana.service.ManagerMainService;
import com.diana.service.ManagerRoleService;
import com.diana.service.QueueMainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-04-21
 */
@Service
public class ManagerMainServiceImpl extends ServiceImpl<ManagerMainMapper, ManagerMain> implements ManagerMainService {

    @Autowired
    private ManagerRoleService managerRoleService;
    @Autowired
    private ManagerMainMapper managerMainMapper;
    @Autowired
    private QueueMainService queueMainService;
    @Value("${defaultHeadImage}")
    private String defaultHeadImage;

    private static Logger log = LoggerFactory.getLogger(ManagerMainServiceImpl.class);
    /**
     * 添加管理员
     * @param map
     * @return
     */
    @Override
    public String addManager(Map<String, Object> map) {
        String loginName = Utils.toString(map.get("loginName"));
        String roleId = Utils.toString(map.get("roleId"));
        String email = Utils.toString(map.get("email"));
        String phone = Utils.toString(map.get("phone"));
        /*if(Utils.isNullOrEmpty(loginName)){
            return Utils.message("0001","请输入登录名",null);
        }*/
        if(!Utils.checkEngStr(loginName,2,10)){
            return Utils.message("0001","请输入正确的登录名，2~10个英文字符",null);
        }
        if(Utils.isNullOrEmpty(roleId)){
            return Utils.message("0001","请选择管理员角色",null);
        }
        if(Utils.isNullOrEmpty(phone)){
            return Utils.message("0001","请输入手机号码",null);
        }
        ManagerMain managerMain = selectOne(Condition.create().eq("loginName", loginName).ne("status", StatusEnums.CommonStatus.删除));
        if(managerMain!=null){
            return Utils.message("0001","登录名已存在，请重新输入",null);
        }
        ManagerRole managerRole = managerRoleService.selectOne(Condition.create().eq("roleId",roleId).eq("status",StatusEnums.CommonStatus.可用));
        if(managerRole==null){
            return Utils.message("0001","角色不可用",null);
        }

        managerMain = new ManagerMain();

        String managerId = queueMainService.getQueueValue("managerId");

        managerMain.setManagerId(managerId);
        managerMain.setLoginName(loginName);
        managerMain.setNikeName(loginName);
        managerMain.setPhone(phone);
        managerMain.setEmail(email);
        managerMain.setPassword(Utils.MD5("123456"));
        managerMain.setRoleId(roleId);
        managerMain.setStatus(StatusEnums.CommonStatus.可用);
        managerMain.setHeadUrl(defaultHeadImage);

        boolean check = insert(managerMain);

        if(check){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 修改基本信息
     * @param map
     * @return
     */
    @Override
    public String updateBaseInfo(Map<String, Object> map) {
        String nikeName = Utils.toString(map.get("nikeName"));
        String password = Utils.toString(map.get("password"));
        String phone = Utils.toString(map.get("phone"));
        String email = Utils.toString(map.get("email"));
        String managerId = Utils.toString(map.get("managerId"));
        String roleId = Utils.toString(map.get("roleId"));
        Integer status = Utils.toInteger(map.get("status"));
        String headUrl = Utils.toString(map.get("headUrl"));
        ManagerMain managerMain = selectOne(Condition.create().eq("managerId", managerId).ne("status", StatusEnums.CommonStatus.删除));
        if(managerMain==null){
            return Utils.message("0001","该管理员不存在",null);
        }
        Integer version = managerMain.getVersion();
        managerMain = new ManagerMain();
        managerMain.setManagerId(managerId);
        if(!Utils.isNullOrEmpty(nikeName)){
            managerMain.setNikeName(nikeName);
        }
        if(!Utils.isNullOrEmpty(roleId)){
            managerMain.setRoleId(roleId);
        }
        if(!Utils.isNullOrEmpty(password)){
            managerMain.setPassword(Utils.MD5(password));
        }
        if(!Utils.isNullOrEmpty(phone)){
            managerMain.setPhone(phone);
        }
        if(!Utils.isNullOrEmpty(email)){
            managerMain.setEmail(email);
        }
        if(!Utils.isNullOrEmpty(headUrl)){
            managerMain.setHeadUrl(headUrl);
        }
        if(status!=null){
            managerMain.setStatus(status);
        }
        managerMain.setVersion(version);
        boolean check = updateById(managerMain);
        if(check){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 修改管理员状态
     * @param map
     * @return
     */
    @Override
    public String updateStatus(Map<String, Object> map) {
        Integer status = Utils.toInteger(map.get("status"));
        String managerId = Utils.toString(map.get("managerId"));
        if(status==null){
            return Utils.message("0001","请选择状态",null);
        }
        ManagerMain managerMain = selectOne(Condition.create().eq("managerId", managerId).ne("status", StatusEnums.CommonStatus.删除));
        if(managerMain==null){
            return Utils.message("0001","该管理员不存在",null);
        }
        managerMain = new ManagerMain();
        managerMain.setStatus(status);
        managerMain.setManagerId(managerId);

        boolean check = updateById(managerMain);
        if(check){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 获取管理员信息
     * @param map
     * @return
     */
    @Override
    public String getManagerInfo(Map<String, Object> map) {
        String managerId = Utils.toString(map.get("managerId"));
        if(Utils.isNullOrEmpty(managerId)){
            return Utils.message("0001","managerId为null",null);
        }
        ManagerMain managerMain = selectOne(Condition.create().eq("managerId", managerId).ne("status", StatusEnums.CommonStatus.删除));
        managerMain.setPassword("");
        return Utils.message("0000","获取成功",managerMain);
    }


    /**
     * 获取管理员列表
     * @param map
     * @return
     */
    @Override
    public String getManagerList(Map<String, Object> map) {
        String nikeName = Utils.toString(map.get("nikeName"));
        String roleId = Utils.toString(map.get("roleId"));
        Integer status = Utils.toInteger(map.get("status"));
        Integer index = Utils.toInteger(map.get("index"));
        Integer size = Utils.toInteger(map.get("size"));

        String joinsql = "";
        if(!Utils.isNullOrEmpty(nikeName)){
            joinsql += "and a.nikeName = '"+nikeName+"'" ;
        }
        if(!Utils.isNullOrEmpty(roleId)){
            joinsql += "and a.roleId = '"+roleId+"'" ;
        }
        if(status!=null){
            joinsql += "and a.status = "+status+"" ;
        }
        Page<Map<String,Object>> page = PageUtils.getPage(index, size);
        List<Map<String, Object>> list = managerMainMapper.getManagerList(joinsql, page);
        page.setRecords(list);
        return Utils.message("0000","获取成功",page.getRecords(),page.getTotal());
    }

    /**
     * 管理员登录
     * @param map
     * @return
     */
    public String login(Map<String, Object> map) {
        String loginName = Utils.toString(map.get("loginName"));
        String password = Utils.toString(map.get("password"));
        if(Utils.isNullOrEmpty(loginName)){
            return Utils.message("0001","请输入登录名",null);
        }
        if(Utils.isNullOrEmpty(password)){
            return Utils.message("0001","请输入密码",null);
        }
        ManagerMain managerMain = selectOne(Condition.create().eq("loginName", loginName).eq("status", StatusEnums.CommonStatus.可用));
        if(managerMain==null){
            return Utils.message("0001","用户不存在或已停用，请联系管理员",null);
        }
        if(!Utils.MD5(password).equals(managerMain.getPassword())){
            return Utils.message("0001","密码错误",null);
        }
        String managerId = managerMain.getManagerId();
        String token = TokenUtils.getToken(managerId,StatusEnums.tokenTime);
        JSONObject info = new JSONObject();
        info.put("managerId",managerId);
        info.put("token",token);
        info.put("nikeName",managerMain.getNikeName());
        return Utils.message("0000","操作成功",info);
    }

    /**
     * 获取当前用户信息
     * @param managerId
     * @return
     */
    @Override
    public String getCurrentManagerInfo(String managerId) {
        ManagerMain managerMain = selectOne(Condition.create().eq("managerId", managerId).ne("status", StatusEnums.CommonStatus.删除));
        managerMain.setPassword("");
        return Utils.message("0000","获取成功",managerMain);
    }
}
